﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Data.Common;
using System.Net.Mail;

public partial class AlertDetail : System.Web.UI.Page
{
    Microsoft.Practices.EnterpriseLibrary.Data.Database db = DatabaseFactory.CreateDatabase("Application_Data");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Convert.ToBoolean(Session["sAdministrator"].ToString()) == false)
            {
                Response.Redirect("~/AccessDenied.aspx");
            }
            BindData();
        }
    }
    private void BindData()
    {
        string sqlQuery1 = "SELECT * FROM Alert WHERE AlertID = '" + Request.QueryString["AlertID"].ToString() + "'";
        DataSet ds1 = db.ExecuteDataSet(CommandType.Text, sqlQuery1);
        DataRow dr1 = ds1.Tables[0].Rows[0];
        
        this.lnkInstance.NavigateUrl = "~/InstanceDetail.aspx?InstanceName=" + dr1["AlertInstanceName"].ToString();
        this.lnkInstance.Text = dr1["AlertInstanceName"].ToString(); 

        this.lblAlertPriority.Text = dr1["AlertPriority"].ToString();
        this.lblAlert.Text = dr1["AlertName"].ToString();
        this.lblAlertLastOccurance.Text = dr1["AlertLastOccurance"].ToString();
        this.txtResolution.Text = dr1["AlertResolution"].ToString();
        this.lblAlertStatus.Text = dr1["AlertStatus"].ToString(); 
        this.lblCount.Text = dr1["AlertCount"].ToString();
    }  
    
    protected void btnAcknowledge_Click(object sender, EventArgs e)
    {
        String strTimeDisplay1 = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time1DisplayName'").ToString();
        String strTimeDisplay2 = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time2DisplayName'").ToString();
        String strTimeDisplay3 = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time3DisplayName'").ToString();
        String strTimeDisplay4 = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time4DisplayName'").ToString();

        String strTimeZone1 = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time1TimeZoneID'").ToString();
        String strTimeZone2 = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time2TimeZoneID'").ToString();
        String strTimeZone3 = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time3TimeZoneID'").ToString();
        String strTimeZone4 = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time4TimeZoneID'").ToString();

        DateTimeOffset nowDateTime = DateTimeOffset.Now;
        DateTimeOffset Time1DateTime = TimeZoneInfo.ConvertTime(nowDateTime, TimeZoneInfo.FindSystemTimeZoneById(strTimeZone1.ToString()));
        DateTimeOffset Time2DateTime = TimeZoneInfo.ConvertTime(nowDateTime, TimeZoneInfo.FindSystemTimeZoneById(strTimeZone2.ToString()));
        DateTimeOffset Time3DateTime = TimeZoneInfo.ConvertTime(nowDateTime, TimeZoneInfo.FindSystemTimeZoneById(strTimeZone3.ToString()));
        DateTimeOffset Time4DateTime = TimeZoneInfo.ConvertTime(nowDateTime, TimeZoneInfo.FindSystemTimeZoneById(strTimeZone4.ToString()));

        if (this.txtResolution.Text.Length <= 0)
        {
            this.txtResolution.Text = "Actioned By: " + Session["sFullUserName"].ToString();
        }

        string sqlQuery1 = "UPDATE Alert SET AlertStatus = 'ACKNOWLEDGED', AlertResolution='" + this.txtResolution.Text.ToString() + "' WHERE AlertID = '" + Request.QueryString["AlertID"].ToString() + "'";
        db.ExecuteNonQuery(CommandType.Text, sqlQuery1);

        MailMessage msg = new MailMessage();

        msg.To.Add(Utility.DBANotificationEMailString());
        msg.Subject = "DBA Alert: Alert Acknowledgement";
        msg.Body = "-----------------------------------------------------" + " \r\n" +
                   "       Alert   : SQL Alert Acknowledgement" + " \r\n" +
                   "-----------------------------------------------------" + " \r\n" +
                   " SQL Instance  : " + this.lnkInstance.Text.ToString() + " \r\n" +
                   "    SQL Alert  : " + this.lblAlert.Text.ToString() + " \r\n" +
                   "     Comments  : " + this.txtResolution.Text.ToString() + " \r\n" +
                   " " + " \r\n" +
                   "-----------------------------------------------------" + " \r\n" +
                   " Actioned By   : " + Session["sFullUserName"].ToString() + " \r\n" +
                   "-----------------------------------------------------" + " \r\n" +
                   " " + strTimeDisplay1.ToString().PadRight(13) + " : " + Time1DateTime.DateTime.ToString() + " \r\n" +
                   " " + strTimeDisplay2.ToString().PadRight(13) + " : " + Time2DateTime.DateTime.ToString() + " \r\n" +
                   " " + strTimeDisplay3.ToString().PadRight(13) + " : " + Time3DateTime.DateTime.ToString() + " \r\n" +
                   " " + strTimeDisplay4.ToString().PadRight(13) + " : " + Time4DateTime.DateTime.ToString() + " \r\n" +
                   "-----------------------------------------------------" + " \r\n" +
                   "END OF MESSAGE" + " \r\n";

        SmtpClient client = new SmtpClient();
        client.Send(msg);

        Response.Redirect("~/Dashboard.aspx");

        
    }
    protected void btnResolve_Click(object sender, EventArgs e)
    {
        String strTimeDisplay1 = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time1DisplayName'").ToString();
        String strTimeDisplay2 = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time2DisplayName'").ToString();
        String strTimeDisplay3 = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time3DisplayName'").ToString();
        String strTimeDisplay4 = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time4DisplayName'").ToString();

        String strTimeZone1 = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time1TimeZoneID'").ToString();
        String strTimeZone2 = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time2TimeZoneID'").ToString();
        String strTimeZone3 = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time3TimeZoneID'").ToString();
        String strTimeZone4 = db.ExecuteScalar(CommandType.Text, "SELECT ConfigValue FROM Config Where ConfigName ='Time4TimeZoneID'").ToString();

        DateTimeOffset nowDateTime = DateTimeOffset.Now;
        DateTimeOffset Time1DateTime = TimeZoneInfo.ConvertTime(nowDateTime, TimeZoneInfo.FindSystemTimeZoneById(strTimeZone1.ToString()));
        DateTimeOffset Time2DateTime = TimeZoneInfo.ConvertTime(nowDateTime, TimeZoneInfo.FindSystemTimeZoneById(strTimeZone2.ToString()));
        DateTimeOffset Time3DateTime = TimeZoneInfo.ConvertTime(nowDateTime, TimeZoneInfo.FindSystemTimeZoneById(strTimeZone3.ToString()));
        DateTimeOffset Time4DateTime = TimeZoneInfo.ConvertTime(nowDateTime, TimeZoneInfo.FindSystemTimeZoneById(strTimeZone4.ToString()));

        if (this.txtResolution.Text.Length <= 0)
        {
            this.txtResolution.Text = "Resolved By: " + Session["sFullUserName"].ToString();
        }

        string sqlQuery1 = "UPDATE Alert SET AlertStatus = 'RESOLVED', AlertResolution='" + this.txtResolution.Text.ToString() + "' WHERE AlertID = '" + Request.QueryString["AlertID"].ToString() + "'";
        db.ExecuteNonQuery(CommandType.Text, sqlQuery1);

        String strAlertAddressesAvailable = db.ExecuteScalar(CommandType.Text, "SELECT COUNT(*) FROM [DBAContact] WHERE [DBAOnCallFlag] = 1 or [DBANotificationFlag] = 1 ").ToString();

        if (strAlertAddressesAvailable == "0")
        {
            Response.Redirect("~/Dashboard.aspx");
        }
        else
        {
            MailMessage msg = new MailMessage();


            msg.To.Add(Utility.DBANotificationEMailString());
            msg.Subject = "DBA Alert: Alert Resolved";
            msg.Body = "-----------------------------------------------------" + " \r\n" +
                       "       Alert : SQL Alert Resolved" + " \r\n" +
                       "-----------------------------------------------------" + " \r\n" +
                       " SQL Instance: " + this.lnkInstance.Text.ToString() + " \r\n" +
                       "    SQL Alert: " + this.lblAlert.Text.ToString() + " \r\n" +
                       "     Comments: " + this.txtResolution.Text.ToString() + " \r\n" +
                       " " + " \r\n" +
                       "-----------------------------------------------------" + " \r\n" +
                       " Actioned By   : " + Session["sFullUserName"].ToString() + " \r\n" +
                       "-----------------------------------------------------" + " \r\n" +
                       " " + strTimeDisplay1.ToString().PadRight(13) + " : " + Time1DateTime.DateTime.ToString() + " \r\n" +
                       " " + strTimeDisplay2.ToString().PadRight(13) + " : " + Time2DateTime.DateTime.ToString() + " \r\n" +
                       " " + strTimeDisplay3.ToString().PadRight(13) + " : " + Time3DateTime.DateTime.ToString() + " \r\n" +
                       " " + strTimeDisplay4.ToString().PadRight(13) + " : " + Time4DateTime.DateTime.ToString() + " \r\n" +
                       "-----------------------------------------------------" + " \r\n" +
                       "END OF MESSAGE" + " \r\n";

            SmtpClient client = new SmtpClient();
            client.Send(msg);

            Response.Redirect("~/Dashboard.aspx");
        }
    }
}
